package com.levi.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;

/**
 * Created by Levi on 2018/3/19.
 */

public class StringUtils {

    /**
     * @param strs
     * @return
     */
    public static boolean isEmpty(String... strs) {
        if (strs == null) {
            return true;
        }
        for (String str : strs) {
            if (!TextUtils.isEmpty(str)) {
                return false;
            }
        }
        return true;
    }

    public static String removeBlank(String str) {
        if (!TextUtils.isEmpty(str)) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            return m.replaceAll("");
        }
        return "";
    }

    /**
     * 手机号校验
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
//        return mobile != null && mobile.matches("1[1-9]\\d{9}");
        return MobileUtil.checkPhone(mobile);
    }

    /**
     * 手机号添加掩码
     *
     * @param mobile
     * @return
     */
    public static String maskMobile(String mobile) {
        if (isMobile(mobile)) {
            return mobile.replace(mobile.substring(3, 7), "****");
        } else {
            return mobile == null ? "" : mobile;
        }
    }

    public static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_DATE = "yyyy-MM-dd";

    /**
     * 将时间格式化为string
     *
     * @param pattern
     * @param time
     * @return
     */
    public static String formatTime(@NonNull String pattern, long time) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date(time));
    }

    public static long string2Date(@NonNull String pattern, @NonNull String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        try {
            Date date = formatter.parse(dateStr);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    private static String HEX_STRING = "0123456789abcdef";

    /**
     * 字符串转16进制字符串
     *
     * @param str
     * @return
     */
    public static String string2Hex(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
//        char[] chars = str.toCharArray();
//        StringBuffer hex = new StringBuffer();
//        for (int i = 0; i < chars.length; i++) {
//            hex.append(Integer.toHexString((int) chars[i]));
//        }
//        return hex.toString();
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(HEX_STRING.charAt((bytes[i] & 0xf0) >> 4));
            sb.append(HEX_STRING.charAt((bytes[i] & 0x0f)));
        }
        return sb.toString();
    }
}
